// Copyright (C) 2013 Michael Leviston (michael.leviston@gmail.com)

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef ECS_ENTITY_H
#define ECS_ENTITY_H

#include <memory>
#include <map>

#include "Component.h"

namespace ECS
{
	// Pointer Definitions
	class Entity;
	typedef std::tr1::shared_ptr<Entity> spEntity;
	typedef std::tr1::weak_ptr<Entity> wpEntity;

	/// \brief  An entity stores components.
	///
	///	Entities store components by their type (i.e. each entity may only have one instance of each type of component)
	/// \author Michael Leviston
	class Entity
	{
	public:

		/// Constructor
		Entity(unsigned int ID) 
			: id(ID), bMarkedForRemoval(false) 
		{
		}

		/// Destructor (all components should be removed by the world before this is called)
		~Entity()
		{
		}
	
		/// Get the Unique ID of this entity
		unsigned int GetID() { return id; }

		/// Mark the entity for removal from the world
		void MarkForRemoval()  { bMarkedForRemoval = true; }

		/// Clear the map of child components
		void ClearComponents() { components.clear(); }

		/// Get a pointer to the map of components
		std::map<unsigned int, spComponent>* GetComponents() { return &components; }

		/// Add a component to the entity
		bool AddComponent(spComponent spC) 
		{ 
			auto itF = components.find(spC->GetType() );
			if(itF == components.end() )
			{
				components[spC->GetType()] = spC;
				return true;
			}
			else
				return false;
		}

		/// Remove a component by Unique ID
		bool RemoveComponentByID(unsigned int ID)
		{
			// Only removing one element using iterator
			auto itB = components.begin();
			auto itE = components.end();

			for(itB; itB != itE; ++itB)		
			{
				if(itB->second->GetID() == ID)
				{
					components.erase(itB);
					return true;
				}	
			}
			return false;
		}

		/// Remove a component by Type
		template <class T>
		unsigned int RemoveComponentByType();

		/// Get a weak pointer to a component by type
		template <class T>
		std::tr1::weak_ptr<T> Get();

		/// Query the entity to see if it has component of Type T
		template <class T>
		bool Has();

		/// Query the entity to see if it has components of Types T, U
		template <class T, class U>
		bool Has();

		/// /// Query the entity to see if it has components of Types T, U, V
		template <class T, class U, class V>
		bool Has();

	// --------------------------------------------------------------------------------
	// Member Variables
	private:
		bool bMarkedForRemoval;		///< Is the entity marked for removal
		std::map<unsigned int, spComponent> components;	/// Map of components (stored by Type)
		unsigned int id;			///< Unique ID of this entity
	};

	template <class T>
	unsigned int Entity::RemoveComponentByType()
	{
		unsigned int id;
		auto itF = components.find(T::type);
		if (itF != components.end())
		{
			spComponent spC = itF->second;
			id = spC->GetID();
			components.erase(itF);
		}
		else
			id = 0;

		return id;
	}

	template <class T>
	std::tr1::weak_ptr<T> Entity::Get()
	{
		auto findIt = components.find(T::type);
		if (findIt != components.end())
		{
			spComponent pBase(findIt->second);
			std::tr1::shared_ptr<T> pSub(std::static_pointer_cast<T>(pBase));  // cast to subclass version of the pointer
			std::tr1::weak_ptr<T> pWeakSub(pSub);  // convert strong pointer to weak pointer
			return pWeakSub;  // return the weak pointer
		}
		else
		{
			return std::tr1::weak_ptr<T>();
		}
	}

	template <class T>
	bool Entity::Has()
	{
		auto findIt = components.find(T::type);
		if (findIt != components.end())
		{
			return true;
		}
		else
			return false;
	}

	template <class T, class U>
	bool Entity::Has()
	{
		auto itE = components.end();

		auto itF = components.find(T::type);
		if (itF != itE)
		{
			itF = components.find(U::type);
			if (itF != itE)
			{
				return true;
			}
			else
				return false;
		}
		else
			return false;
	}

	template <class T, class U, class V>
	bool Entity::Has()
	{
		auto itE = components.end();

		auto itF = components.find(T::type);
		if (itF != itE)
		{
			itF = components.find(U::type);
			if (itF != itE)
			{
				itF = components.find(V::type);
				if (itF != itE)
				{
					return true;
				}
				else
					return false;
			}
			else
				return false;
		}
		else
			return false;
	}
}

#endif // ECS_ENTITY_H